package weaver.interfaces.zhlx.duss.action.salesmodule.ec;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @author dusongsong
 * @date 2023年10月16日 13:19
 *  客户建档审批表流程生成三位检索码
 */
public class WeaKhjdsplcSwmAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        writeLog(">>>>>WeaBjdlcBackAction>>>>>requestid=" + requestid);
        try {
            RecordSet rs = new RecordSet();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String khywjc = Util.null2String(rs.getString("khywjc"));
            String khywjcszm = Util.null2String(rs.getString("khywjcszm"));
            String qym1 = Util.null2String(rs.getString("qym1"));
            String qym2 = Util.null2String(rs.getString("qym2"));
            String khssc1 = Util.null2String(rs.getString("khssc1"));
            String crmkhbh = Util.null2String(rs.getString("crmkhbh"));
            if (!"".equals(khywjc) && !"".equals(khywjcszm) && "".equals(khssc1)) {
                if (!"".equals(qym1) && "".equals(qym2)) {
                    JSONObject dataJson = generateSwm(khywjcszm, qym1, khywjc,crmkhbh);
                    String status = Util.null2String(dataJson.getString("status"));
                    String message = Util.null2String(dataJson.getString("message"));
                    if ("success".equals(status)) {
                        rs.executeUpdate("update "+tablename+" set khssc1=? where requestid=?",message,requestid);
                    }
                }
                if (!"".equals(qym1) && !"".equals(qym2)) {
                    JSONObject data1Json = generateSwm(khywjcszm, qym1, khywjc,crmkhbh);
                    String status1 = Util.null2String(data1Json.getString("status"));
                    String message1 = Util.null2String(data1Json.getString("message"));
                    if ("success".equals(status1)) {
                        rs.executeUpdate("update "+tablename+" set khssc1=? where requestid=?",message1,requestid);
                    } else {
                        if ("allfull".equals(status1)) {
                            JSONObject data2Json = generateSwm(khywjcszm, qym2, khywjc,crmkhbh);
                            String status2 = Util.null2String(data2Json.getString("status"));
                            String message2 = Util.null2String(data2Json.getString("message"));
                            if ("success".equals(status2)) {
                                rs.executeUpdate("update "+tablename+" set khssc1=? where requestid=?",message2,requestid);
                            }
                        }
                    }
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaBjdlcBackAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

    private static JSONObject generateSwm(String fname, String areacode, String fullname,String crmkhbh) {
        BaseBean baseBean = new BaseBean();
        JSONObject dataJson = new JSONObject();
        String status = "";
        String message = "";
        if (!"".equals(fname) && !"".equals(areacode) && !"".equals(fullname)) {
            String shortCodeA9Z = getRuleToA9Z(fname, areacode, fullname,crmkhbh);
            baseBean.writeLog(">>>>>getSwjsm_Ajax>>>>>shortCodeA9Z=" + shortCodeA9Z);
            if (!"".equals(shortCodeA9Z)) {
                status = "success";
                message = shortCodeA9Z;
            } else {
                String shortCodeAA9 = getRuleToAA9(fname, areacode, fullname,crmkhbh);
                baseBean.writeLog(">>>>>getSwjsm_Ajax>>>>>shortCodeAA9=" + shortCodeAA9);
                if (!"".equals(shortCodeAA9)) {
                    status = "success";
                    message = shortCodeAA9;
                } else {
                    String shortCode9AZ = getRuleTo9AZ(fname, areacode, fullname,crmkhbh);
                    baseBean.writeLog(">>>>>getSwjsm_Ajax>>>>>shortCode9AZ=" + shortCode9AZ);
                    if (!"".equals(shortCode9AZ)) {
                        status = "success";
                        message = shortCode9AZ;
                    } else {
                        status = "allfull";
                        message = "已经全部占用了";
                    }
                }
            }
        } else {
            status = "paramter_error";
            message = "参数为空或者不全";
        }
        dataJson.put("status", status);
        dataJson.put("message", message);
        return dataJson;
    }

    private static String getRuleToA9Z(String fname, String areacode, String fullname,String crmkhbh) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select * from (select id,shortCode from uf_swjsm where billing='A9Z' and Area=? and FirstChar=?" +
                " and (used='' or used is null) order by serial) a where rownum=1", areacode, fname);
        rs.next();
        String id = Util.null2String(rs.getString("id"));
        String shortCode = Util.null2String(rs.getString("shortCode"));
        if (!"".equals(id)) {
            rs.executeUpdate("update uf_swjsm set used=?,customercode=? where id=?", fullname,crmkhbh, id);
        }
        return shortCode;
    }

    private static String getRuleToAA9(String fname, String areacode, String fullname,String crmkhbh) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select * from (select id,shortCode from uf_swjsm where billing='AA9' and Area=? and FirstChar=?" +
                " and (used='' or used is null) order by serial) a where rownum=1", areacode, fname);
        rs.next();
        String id = Util.null2String(rs.getString("id"));
        String shortCode = Util.null2String(rs.getString("shortCode"));
        if (!"".equals(id)) {
            rs.executeUpdate("update uf_swjsm set used=?,customercode=? where id=?", fullname,crmkhbh, id);
        }
        return shortCode;
    }

    private static String getRuleTo9AZ(String fname, String areacode, String fullname,String crmkhbh) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select * from (select id,shortCode from uf_swjsm where billing='9AZ' and Area=? and FirstChar=?" +
                " and (used='' or used is null) order by serial) a where rownum=1", areacode, fname);
        rs.next();
        String id = Util.null2String(rs.getString("id"));
        String shortCode = Util.null2String(rs.getString("shortCode"));
        if (!"".equals(id)) {
            rs.executeUpdate("update uf_swjsm set used=?,customercode=? where id=?", fullname,crmkhbh, id);
        }
        return shortCode;
    }
}
